package com.freelancer.musicinstruction.activity.delegates;

import com.freelancer.musicinstruction.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;


public class DialogManager {
	
	private static final int DIALOG_YES_NO_MESSAGE = 1;
    private static final int DIALOG_YES_NO_LONG_MESSAGE = 2;
    private static final int DIALOG_LIST = 3;
    private static final int DIALOG_PROGRESS = 4;
    private static final int DIALOG_SINGLE_CHOICE = 5;
    private static final int DIALOG_MULTIPLE_CHOICE = 6;
    private static final int DIALOG_TEXT_ENTRY = 7;
    private static final int DIALOG_CUSTOM = 8;

    
	private ProgressDialog progressDialog;
	private int progress;
	private Handler progressHandler;
	
	private String customOkDialogTitle;
	private String customOkDialogMessage;
	
	private Activity activity;
	
	public DialogManager( Activity activity ) {
		this.activity = activity;
	}

	

	public void showProgressDialog(Context context, String title, String message) {
		progressDialog = ProgressDialog.show(context, title, message, true);
		

	}

	public void closeProgressDialog() {
		if (progressDialog != null) {
			if (progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
		}

	}

	
	public Dialog onCreateDialog(int dialogId) {

		int title;
		int message;

		switch (dialogId) {

			case DIALOG_YES_NO_MESSAGE:
	            return new AlertDialog.Builder(activity)
	                .setTitle("Alert_Two_Buttons")
	                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	
	                        /* User clicked OK so do some stuff */
	                    }
	                })
	                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	
	                        /* User clicked Cancel so do some stuff */
	                    }
	                })
	                .create();
			case DIALOG_YES_NO_LONG_MESSAGE:
	            return new AlertDialog.Builder(activity)	              
	                .setTitle("Alert_Two_Buttons_Long_Message")
	                .setMessage("Yes_No_Long_Message")
	                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {

	                        /* User clicked OK so do some stuff */
	                    }
	                })
	                .setNeutralButton("Something", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {

	                        /* User clicked Something so do some stuff */
	                    }
	                })
	                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {

	                        /* User clicked Cancel so do some stuff */
	                    }
	                })
	                .create();
			case DIALOG_LIST:
	            return new AlertDialog.Builder(activity)
	                .setTitle("Alert List")
	                .setItems(R.array.instruments, new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {

	                        /* User clicked so do some stuff */
	                        String[] items = activity.getResources().getStringArray(R.array.instruments);
	                        new AlertDialog.Builder(activity)
	                                .setMessage("You selected: " + which + " , " + items[which])
	                                .show();
	                    }
	                })
	                .create();
	        
			case DIALOG_PROGRESS:
				progressDialog = new ProgressDialog(activity);
				progressDialog.setTitle("Progress..");
				progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				progressDialog.setButton("Yes", new DialogInterface.OnClickListener() {
	                public void onClick(DialogInterface dialog, int whichButton) {

	                    /* User clicked Yes so do some stuff */
	                }
	            });
				progressDialog.setButton2("No", new DialogInterface.OnClickListener() {
	                public void onClick(DialogInterface dialog, int whichButton) {

	                    /* User clicked No so do some stuff */
	                }
	            });
	            return progressDialog;
	        case DIALOG_SINGLE_CHOICE:
	            return new AlertDialog.Builder(activity)
	                .setTitle("Dialog Single Choice")
	                .setSingleChoiceItems(R.array.instruments, 0, new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {

	                        /* User clicked on a radio button do some stuff */
	                    }
	                })
	                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {

	                        /* User clicked Yes so do some stuff */
	                    }
	                })
	                .setNegativeButton("No", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {

	                        /* User clicked No so do some stuff */
	                    }
	                })
	               .create();
	        case DIALOG_MULTIPLE_CHOICE:
	            return new AlertDialog.Builder(activity)
	                .setTitle("Multiple Choice Dialog")
	                .setMultiChoiceItems(R.array.instruments,
	                        new boolean[]{false, true},
	                        new DialogInterface.OnMultiChoiceClickListener() {
	                            public void onClick(DialogInterface dialog, int whichButton,
	                                    boolean isChecked) {

	                                /* User clicked on a check box do some stuff */
	                            }
	                        })
	                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {

	                        /* User clicked Yes so do some stuff */
	                    }
	                })
	                .setNegativeButton("No", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {

	                        /* User clicked No so do some stuff */
	                    }
	                })
	               .create();
	        case DIALOG_TEXT_ENTRY:
	            // This example shows how to add a custom layout to an AlertDialog
	            LayoutInflater factory = LayoutInflater.from(activity);
	            final View textEntryView = factory.inflate(0, null);
	            return new AlertDialog.Builder(activity)
	                .setTitle("Dialog Text Entry")
	                .setView(textEntryView)
	                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {

	                        /* User clicked OK so do some stuff */
	                    }
	                })
	                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {

	                        /* User clicked cancel so do some stuff */
	                    }
	                })
	                .create();

	
			case DIALOG_CUSTOM:
				return new AlertDialog.Builder(activity).setTitle(
						customOkDialogTitle).setMessage(customOkDialogMessage)
						.setNeutralButton("OK", null).create();
			default:
				title = R.string.dialog_unknown_error_title;
				message = R.string.dialog_unknown_error_message;
		}

		return new AlertDialog.Builder(activity).setTitle(title).setMessage(
				message).setNeutralButton(R.string.dialog_OK_button, null)
				.create();

	}

	public void showCustomDialog(String title, String message) {
		customOkDialogTitle = title;
		customOkDialogMessage = message;
		activity.showDialog(DialogManager.DIALOG_CUSTOM);
	}
	
	protected void setActivity( Activity activity ){
		this.activity = activity;
	}
}
